#!/usr/bin/env python

import time

from kivy.logger import Logger
from kivy.uix.tabbedpanel import TabbedPanelHeader

from amgineirc.view.messageView import MessageView
from amgineirc.viewModel.channels.tabbedPanelHeaderViewModel import *

__all__ = ['ServerTabbedPanelHeaderView',
           'ChannelTabbedPanelHeaderView',
           'PrivmsgTabbedPanelHeaderView',
           'DccTabbedPanelHeaderView']

class _LongTouchTabbedPanelHeaderView(TabbedPanelHeader, MessageView):
    def __init__(self, vm, **kargs):
        TabbedPanelHeader.__init__(self, **kargs)
        MessageView.__init__(self)
        self._view_model = vm
        self.touch_down_time = 0.0
        self.touch_up_time= 0.0
        self.bind(on_touch_down=self.handle_on_touch_down,
                  on_touch_up = self.handle_on_touch_up)
        self.register_event_type('on_long_touch')

    def on_long_touch(self, *largs):
        self._view_model.handle_on_long_touch(*largs)
    
    def handle_on_touch_down(self, stuff, touch):
        if self.collide_point(*touch.pos):
            self.touch_down_time = time.time()
        

    def handle_on_touch_up(self, stuff, touch):
        if self.collide_point(*touch.pos):
            self.touch_up_time = time.time()
            duration = self.touch_up_time - self.touch_down_time
            if duration > 0.2:
                self.dispatch('on_long_touch')

class ServerTabbedPanelHeaderView(_LongTouchTabbedPanelHeaderView):

    def __init__(self, panel, server, **kargs):
        vm = ServerTabbedPanelHeaderViewModel(self, panel)
        _LongTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = server


class ChannelTabbedPanelHeaderView(_LongTouchTabbedPanelHeaderView):
    
    def __init__(self, panel, server, target, **kargs):
        vm = ChannelTabbedPanelHeaderViewModel(self, panel)
        _LongTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = target

class PrivmsgTabbedPanelHeaderView(_LongTouchTabbedPanelHeaderView):

    def __init__(self, panel, server, nick, **kargs):
        vm = PrivmsgTabbedPanelHeaderViewModel(self, panel)
        _LongTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = nick

class DccTabbedPanelHeaderView(_LongTouchTabbedPanelHeaderView):

    def __init__(self, panel, peer_ip, peer_nick, **kargs):
        vm = DccTabbedPanelHeaderViewModel(self, panel)
        _LongTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._peer_ip = peer_ip
        self._peer_nick = peer_nick
